package com.example.book.cms.controller;

import com.example.book.base.BaseController;
import com.example.book.base.Result;
import com.example.book.common.shiro.ShiroUtils;
import com.example.book.common.utils.LocalFileUtils;
import com.example.book.domain.sys.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import com.example.book.cms.entity.CmsCarousel;
import com.example.book.cms.service.CmsCarouselService;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author   hcj
 * @date 2019-12-15 20:35:33
*/
@Controller
@RequestMapping(value = "/sys/cmsCarousel")
public class CmsCarouselController extends BaseController{

    @Autowired
    private CmsCarouselService cmsCarouselService;
    @Value("${system.profile.path}")
    private String profile;

    @Value("${system.profile.real_path}")
    private String realProfile;


    @ModelAttribute
    public CmsCarousel get(@RequestParam(required=false) String id) {
        if (StringUtils.isNotBlank(id)){
            CmsCarousel cmsCarousel = new CmsCarousel();
            cmsCarousel.setId(id);
            return cmsCarouselService.get(cmsCarousel);
        }else{
            return new CmsCarousel();
        }
    }

    /**
     * 新增修改页面
     *
     * @param carousel
     * @param model
     * @return
     */
    @GetMapping("/form")
    public String form(CmsCarousel carousel, Model model) {
        if (carousel != null) {
            model.addAttribute("carousel", carousel);
        }
        return "cms/carousel_form";
    }

    @RequestMapping(value = "/get")
    @ResponseBody
    public CmsCarousel getById(CmsCarousel cmsCarousel) {
        return cmsCarousel;
    }

    @RequestMapping(value = "/list")
    public String list(CmsCarousel cmsCarousel) {
        return "cms/carousel_list";
    }

    @RequestMapping(value = "/info")
    @ResponseBody
    public List<CmsCarousel> info(CmsCarousel cmsCarousel) {
        return cmsCarouselService.findList(cmsCarousel);
    }


    @RequestMapping(value = "/findPage")
    @ResponseBody
    public PageInfo<CmsCarousel> findPage(CmsCarousel cmsCarousel) {
        return  cmsCarouselService.findPage( cmsCarousel);
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(CmsCarousel cmsCarousel, MultipartFile file) throws IOException {

        if (!file.isEmpty()) {
            SysUser sysUser = ShiroUtils.getUser();
            String path = LocalFileUtils.upload(sysUser.getId(), profile, realProfile, file);
            cmsCarousel.setPictureUrl(path);
        }

        cmsCarouselService.save(cmsCarousel);
        return Result.success("保存成功");
    }

    @RequestMapping(value = "/logicalDeletion",method = RequestMethod.POST)
    @ResponseBody
    public Result logicalDeletion(@RequestParam("id") String id){
        cmsCarouselService.logicalDeletion(id);
        return Result.success();
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestParam("ids") String[] ids){
        cmsCarouselService.delete(ids);
        return Result.success();
    }

}